﻿using System;
using System.Xml;
using JetBrains.ReSharper.TaskRunnerFramework;
using Objectware.Silverlight.Testing.MetaData;
using Objectware.Silverlight.Testing.Runner.Resharper.Elements;
using Objectware.Silverlight.Testing.Runner.Utilities;

namespace Objectware.Silverlight.Testing.Runner.Resharper.Tasks
{
	[Serializable]
	public class MethodTask : RemoteTask, IEquatable<MethodTask>
	{

		public MethodTask(XmlElement element)
			: base(element)
		{
			this.TestMethod = GetXmlAttribute(element, "TestMethod");
			this.TestType = GetXmlAttribute(element, "TestType");
			this.Explicitly = GetXmlAttribute(element, "Explicitly") == "true";
			this.ProjectPath = GetXmlAttribute(element, "ProjectPath");
			this.XapPath = GetXmlAttribute(element, "XapPath");
			/*
			this.ActualTestMethod = 
				SerializationHelper.
					DeserializeFromString<MethodTestElement>(GetXmlAttribute(element, "ActualTestMethod"));
			 * */
		}

		public MethodTask(string testType,
							string testMethod,
							MethodTestElement actualMethod,
							bool explicitly,
							string projectPath,
							string xapPath)
			: base("Silverlight")
		{
			if (testMethod == null)
				throw new ArgumentNullException("testMethod");

			if (testType == null)
				throw new ArgumentNullException("testType");

			this.TestType = testType;
			this.TestMethod = testMethod;
			this.Explicitly = explicitly;
			//this.ActualTestMethod = actualMethod;
			this.ProjectPath = projectPath;
			this.XapPath = xapPath;
		}

		public bool Explicitly { get; private set; }

		public string FullyQualifiedTestMethod
		{
			get
			{
				return this.TestClassName + "." + this.TestMethod;
			}
		}

		public string TestClassName
		{
			get
			{
				var lastDot = this.TestType.LastIndexOf(".");
				return this.TestType.Substring(lastDot + 1);
			}
		}

		public string TestMethod { get; private set; }
		public string TestType { get; private set; }

		public string ProjectPath { get; private set; }
		public string XapPath { get; private set; }

		//public MethodTestElement ActualTestMethod { get; private set; }


		public bool Equals(MethodTask methodTask)
		{
			if (methodTask == null || !base.Equals(methodTask))
				return false;

			return Equals(TestType, methodTask.TestType) &&
				   Equals(TestMethod, methodTask.TestMethod) &&
				   Equals(ProjectPath, methodTask.ProjectPath) &&
				   Equals(XapPath, methodTask.XapPath) &&
				   Explicitly == methodTask.Explicitly;
		}

		public override bool Equals(object obj)
		{
			return (this == obj || Equals(obj as MethodTask));
		}

		public override void SaveXml(XmlElement element)
		{
			base.SaveXml(element);
			SetXmlAttribute(element, "TestMethod", TestMethod);
			SetXmlAttribute(element, "TestType", TestType);
			SetXmlAttribute(element, "Explicitly", Explicitly ? "true" : "false");
			SetXmlAttribute(element, "ProjectPath", ProjectPath);
			SetXmlAttribute(element, "XapPath", XapPath);
			//SetXmlAttribute(element, "ActualTestMethod", SerializationHelper.SerializeToString(this.ActualTestMethod));
		}
	}
}
